package com.cnpinxi.service;

import com.cnpinxi.dao.OrderItemMapper;
import com.cnpinxi.domain.Order;
import com.cnpinxi.domain.OrderItem;
import com.cnpinxi.domain.ProductEntity;
import com.cnpinxi.domain.User;
import com.cnpinxi.log.LogFactory;
import com.cnpinxi.myRunTime.MyRunTimeExcption;
import com.cnpinxi.service.*;
import com.cnpinxi.service.OrderService;
import com.cnpinxi.service.UserService;
import com.cnpinxi.util.GenerateID;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Baggio on 2017-11-22.
 */
@Service
public class OrderItemServiceImpl  implements com.cnpinxi.service.OrderItemService {
    protected Logger logger = LogFactory.getLogger(getClass());

    @Autowired
    OrderItemMapper orderItemMapper;

    @Autowired
    ProductEntityService productEntityService;
    @Autowired
    com.cnpinxi.service.OrderService orderService;  @Autowired
    UserService userService;

    @Override
    public Boolean modifyOrderItem(OrderItem orderItem) {
  orderItemMapper.updateByPrimaryKeySelective(orderItem);
        return true;
    }

    @Override
    public OrderItem getOrderItemByOrderItemId(Long orderItemId) {
        return orderItemMapper.getOrderItemByOrderItemId(orderItemId);
    }

    @Override
    public List<OrderItem> selectOrderItemList(Map<String,String> map) {
        return orderItemMapper.selectOrderItemList(map);
    }

    @Override
    public int updateOrderItemStatus(Map<String, String> map) {
        return orderItemMapper.updateOrderItemStatus(map);
    }

    @Override
    public OrderItem createOrderItem(Long productEntityId, Order order, Long userId) {

        Map<String, String> map = new HashMap<>();
        map.put("productEntityId", String.valueOf(productEntityId));

        User user=userService.getUserById(userId);
        if(user!=null){
            map.put("openId",user.getOpenId());
        }
        ProductEntity productEntity = productEntityService.getProductEntity(map);

        if(productEntity==null){
            throw new MyRunTimeExcption("产品不存在！");
        }
        //TODO 后面需要加入数量判断

        if (productEntity == null) {
            throw new MyRunTimeExcption("产品不存在");
        }

        OrderItem orderItem = new OrderItem();
        orderItem.setOrderId(order.getOrderId());
        orderItem.setOrderItemId(GenerateID.CreatetID());
        orderItem.setProductEntityId(productEntityId);
        //需要int


        map.clear();
        map.put("userId", String.valueOf(userId));
        map.put("productEntityId", String.valueOf(productEntityId));


        orderItem.setPrice(productEntity.getRealPrice());
        orderItem.setCreateTime(new Date());
        orderItem.setStatus(com.cnpinxi.service.OrderService.UNPAY);
        orderItemMapper.insertOrderItem(orderItem);
        return orderItem;
    }

    @Transactional
    @Override
    public Boolean cancelOrderItem(Long orderItemId) {

        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);
        Long orderId = orderItem.getOrderId();
        orderItem.setStatus(com.cnpinxi.service.OrderService.CANCEL);
        if (orderItemMapper.updateOrderItemById(orderItem) != 1) {
            throw new MyRunTimeExcption("取消失败");
        }

        Order order=orderService.getOrderById(orderId);
        orderService.modifyOrder(order);
        return true;
    }

    @Override
    public Boolean modifyOrderItemStatus(Long orderItemId, Byte status) {

        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);
        orderItem.setStatus(status);
        orderItemMapper.updateOrderItemById(orderItem);

        return true;
    }

    @Override
    public Boolean completeOrderItem(Long orderItemId) {
        logger.info("completeOrderItem");
        OrderItem orderItem = orderItemMapper.selectOrderItemListByOrderItemId(orderItemId);

        if (orderItem == null) {
            throw new MyRunTimeExcption("订单项目没有找到！");
        }
        orderItem.setStatus(com.cnpinxi.service.OrderService.COMPELTEED);


        orderItemMapper.updateOrderItemById(orderItem);

        checkOrderItem(orderItem.getOrderId());
        return true;
    }
    private void checkOrderItem(Long orderId) {
        Order order = orderService.getOrderById(orderId);
        List<OrderItem> orderItemList = order.getOrderItemList();
        for (OrderItem orderItems : orderItemList) {
            if (orderItems.getStatus() != com.cnpinxi.service.OrderService.COMPELTEED && orderItems.getStatus() != com.cnpinxi.service.OrderService.REFUNDED) {
                return;
            }
        }
        order.setStatus(OrderService.CLOSE);
        orderService.modifyOrder(order);
    }
}
